package com.finance.rule.service;

import com.finance.base.enums.DirectionEnum;
import com.finance.framework.cache.CacheSource;
import com.finance.framework.cache.RedisConstnats;
import com.finance.framework.lang.Page;
import com.finance.framework.lang.PageResultBO;
import com.finance.rule.bean.pojo.WeightRangeRule;
import com.finance.rule.dao.WeightRangeMapper;
import com.finance.system.bean.pojo.Region;
import com.finance.system.bean.pojo.Supplier;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by CBWL on 2017/3/17.
 */
@Service
public class WeightRangeService implements IWeightRangeService {

    @Autowired
    private WeightRangeMapper weightRangeMapper;



    @Override
    public PageResultBO<WeightRangeRule> listByPage(Page page) {
        PageResultBO<WeightRangeRule> pageResultBO = new PageResultBO<WeightRangeRule>();
        List<WeightRangeRule>list = weightRangeMapper.listByPage(page);
        pageResultBO.setPageNo(page.getPageNo());
        pageResultBO.setPageSize(page.getPageSize());
        pageResultBO.setTotalRecord(page.getTotalRecord());
        pageResultBO.setDataList(list);
        return pageResultBO;
    }

    @Override
    @Cacheable(value = "rule",cacheResolver = "ruleCacheResolver", key = "'weightrule_'+ '#id'",condition = "#result != null")
    public WeightRangeRule getById(Long id){
        return this.weightRangeMapper.getById(id);
    }

    @Override
    @CacheEvict(value="rule",cacheResolver = "ruleCacheResolver",allEntries = true)
    public boolean add(WeightRangeRule weightRange){
        return weightRangeMapper.add(weightRange) == 1;
    }

    @Override
    @CacheEvict(value="rule",cacheResolver = "ruleCacheResolver",allEntries = true)
    public boolean update(WeightRangeRule weightRange){
        boolean result = this.weightRangeMapper.update(weightRange) == 1;
        return result;
    }

    @Override
    @CacheEvict(value="rule",cacheResolver = "ruleCacheResolver",allEntries = true)
    public boolean del(String uid,List<String>ids) {
        boolean result = this.weightRangeMapper.del(uid,ids) > 0;
        return result;
    }

    @Override
    @Cacheable(value = "rule",cacheResolver = "ruleCacheResolver", key = "'weightrule_supplier_'+ '#supplier.code'",condition = "#result != null")
    public List<WeightRangeRule> getWeightRangeRuleBySupplier(Supplier supplier) {
        return this.weightRangeMapper.getWeightRangeRuleBySupplier(supplier);
    }

    @Override
    @Cacheable(value = "rule",cacheResolver = "ruleCacheResolver", key = "'weightrule_direction_'+ '#directionEnum.value'",condition = "#result != null")
    public List<WeightRangeRule> getWeightRangeRuleByDirection(DirectionEnum directionEnum) {
        return this.weightRangeMapper.getWeightRangeRuleByDirection(directionEnum.getValue());
    }

    @Override
    @Cacheable(value = "rule",cacheResolver = "ruleCacheResolver", key = "'weightrule_regionsd_'+ '#region.code'",condition = "#result != null")
    public List<WeightRangeRule> getWeightRangeRuleBySendRegion(Region region) {
        return this.weightRangeMapper.getWeightRangeRuleBySendRegion(region);
    }

    @Override
    @Cacheable(value = "rule",cacheResolver = "ruleCacheResolver", key = "'weightrule_regionsg_'+ '#region.code'",condition = "#result != null")
    public List<WeightRangeRule> getWeightRangeRuleBySignRegion(Region region) {
        return this.weightRangeMapper.getWeightRangeRuleBySignRegion(region);
    }
}
